﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;
using Microsoft.Build.Framework;
using System.Collections;
using Microsoft.Build.Utilities;
using System.Reflection;
using System.IO;
using Microsoft.SharePoint;

namespace SPALM
{
    public partial class WebApplicationSelector : Form
    {
        public ArrayList taskItems { get; set; }

        public WebApplicationSelector()
        {
            InitializeComponent();
            taskItems = new ArrayList();
        }

        public WebApplicationSelector(string title, string messageText)
        {
            this.InitializeComponent();
            taskItems = new ArrayList();

            this.labelText.Text = messageText;
            this.Text = title;
            this.wizardPicture1.Headline = title;

            LoadWebApplications();
        }


        BackgroundWorker worker = null;
        private void LoadWebApplications()
        {
            this.Cursor = Cursors.WaitCursor;

            this.listView1.Items.Clear();
            this.listView1.Visible = false;

            worker = new BackgroundWorker();
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerAsync();
        }

        private delegate void AddListItemCallback(string name, string displayName);
        private void AddListItem(string name, string displayName)
        {
            if (listView1.InvokeRequired)
            {
                AddListItemCallback d = AddListItem;
                Invoke(d, new object[] { name, displayName });
            }
            else
            {
                ListViewItem lvi = new ListViewItem(name);
                listView1.Items.Add(lvi);

                ListViewItem.ListViewSubItem slvi = new ListViewItem.ListViewSubItem();
                slvi.Text = displayName;
                lvi.SubItems.Add(slvi);
            }
        }


        void worker_DoWork(object sender, DoWorkEventArgs e)
        {

            try
            {
                foreach (SPWebApplication webapp in SPWebService.AdministrationService.WebApplications)
                {
                    try
                    {
                        string url = "";
                        foreach (SPAlternateUrl alterurl in webapp.AlternateUrls)
                        {
                            url = alterurl.IncomingUrl;
                            break;
                        }
                        AddListItem(url, webapp.Name);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception)
            {
            }

            try
            {
                foreach (SPWebApplication webapp in SPWebService.ContentService.WebApplications)
                {
                    try
                    {
                        string url = "";
                        foreach (SPAlternateUrl alterurl in webapp.AlternateUrls)
                        {
                            url = alterurl.IncomingUrl;
                            break;
                        }
                        AddListItem(url, webapp.Name);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.button1.Enabled = true;
            this.Cursor = Cursors.Default;

            if (e.Error != null)
            {
                //Exception ocurred
                textBox_Status.Text = e.Error.ToString();
                panel_Status.Visible = true;
            }
            else
            {
                panel_Status.Visible = false;
                this.listView1.Visible = true;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            taskItems.Clear();
            foreach (ListViewItem s in listView1.CheckedItems)
            {
                TaskItem item = new TaskItem(s.Text);
                item.SetMetadata("Value", s.Text);
                item.SetMetadata("Type", s.Text.GetType().ToString());
                taskItems.Add(item);
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void WebApplicationSelector_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
